/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     sincos_table.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-01
 * @brief
 ********************************************************************************/

#include "sincos_table.h"

static float SinCosTable[91][2] = {
	0.000000, 1.000000,
	0.017452, 0.999848,
	0.034899, 0.999391,
	0.052336, 0.998630,
	0.069756, 0.997564,
	0.087156, 0.996195,
	0.104528, 0.994522,
	0.121869, 0.992546,
	0.139173, 0.990268,
	0.156434, 0.987688,
	0.173648, 0.984808,
	0.190809, 0.981627,
	0.207912, 0.978148,
	0.224951, 0.974370,
	0.241922, 0.970296,
	0.258819, 0.965926,
	0.275637, 0.961262,
	0.292372, 0.956305,
	0.309017, 0.951057,
	0.325568, 0.945519,
	0.342020, 0.939693,
	0.358368, 0.933580,
	0.374607, 0.927184,
	0.390731, 0.920505,
	0.406737, 0.913545,
	0.422618, 0.906308,
	0.438371, 0.898794,
	0.453990, 0.891007,
	0.469472, 0.882948,
	0.484810, 0.874620,
	0.500000, 0.866025,
	0.515038, 0.857167,
	0.529919, 0.848048,
	0.544639, 0.838671,
	0.559193, 0.829038,
	0.573576, 0.819152,
	0.587785, 0.809017,
	0.601815, 0.798636,
	0.615661, 0.788011,
	0.629320, 0.777146,
	0.642788, 0.766044,
	0.656059, 0.754710,
	0.669131, 0.743145,
	0.681998, 0.731354,
	0.694658, 0.719340,
	0.707107, 0.707107,
	0.719340, 0.694658,
	0.731354, 0.681998,
	0.743145, 0.669131,
	0.754710, 0.656059,
	0.766044, 0.642788,
	0.777146, 0.629320,
	0.788011, 0.615661,
	0.798636, 0.601815,
	0.809017, 0.587785,
	0.819152, 0.573576,
	0.829038, 0.559193,
	0.838671, 0.544639,
	0.848048, 0.529919,
	0.857167, 0.515038,
	0.866025, 0.500000,
	0.874620, 0.484810,
	0.882948, 0.469472,
	0.891007, 0.453990,
	0.898794, 0.438371,
	0.906308, 0.422618,
	0.913545, 0.406737,
	0.920505, 0.390731,
	0.927184, 0.374607,
	0.933580, 0.358368,
	0.939693, 0.342020,
	0.945519, 0.325568,
	0.951057, 0.309017,
	0.956305, 0.292372,
	0.961262, 0.275637,
	0.965926, 0.258819,
	0.970296, 0.241922,
	0.974370, 0.224951,
	0.978148, 0.207912,
	0.981627, 0.190809,
	0.984808, 0.173648,
	0.987688, 0.156434,
	0.990268, 0.139173,
	0.992546, 0.121869,
	0.994522, 0.104528,
	0.996195, 0.087156,
	0.997564, 0.069756,
	0.998630, 0.052336,
	0.999391, 0.034899,
	0.999848, 0.017452,
	1.000000, 0.000000};

float tanTable[46] = {
	0.000000,
	0.017455,
	0.034920,
	0.052407,
	0.069926,
	0.087488, // 5
	0.105104,
	0.122784,
	0.140540,
	0.158384,
	0.176326, // 10
	0.194380,
	0.212556,
	0.230868,
	0.249328,
	0.267949, // 15
	0.286745,
	0.305730,
	0.324919,
	0.344327,
	0.363970, // 20
	0.383864,
	0.404026,
	0.424474,
	0.445228,
	0.466307, // 25
	0.487732,
	0.509525,
	0.531709,
	0.554309,
	0.577350, // 30
	0.600840,
	0.624869,
	0.649407,
	0.674508,
	0.700207, // 35
	0.726542,
	0.753554,
	0.781285,
	0.809784,
	0.839099,
	0.869286,
	0.900400,
	0.932515,
	0.965688,
	1};

int32_t rangeAngle(int32_t angle, int32_t min, int32_t max)
{
	while (angle < min)
		angle += (max - min);
	while (angle > max)
		angle -= (max - min);
	return angle;
}

float fast_sin(int32_t angle)
{
	angle = rangeAngle(angle, 0, 360);

	if (angle >= 0 && angle <= 90)
		return SinCosTable[angle][0];
	else if (angle >= 90 && angle <= 180)
		return SinCosTable[180 - angle][0];
	else if (angle >= 180 && angle <= 270)
		return -SinCosTable[angle - 180][0];
	else
		return -SinCosTable[360 - angle][0];
}

float fast_cos(int32_t angle)
{
	angle = rangeAngle(angle, 0, 360);

	if (angle >= 0 && angle <= 90)
		return SinCosTable[angle][1];
	else if (angle >= 90 && angle <= 180)
		return -SinCosTable[180 - angle][1];
	else if (angle >= 180 && angle <= 270)
		return -SinCosTable[angle - 180][1];
	else
		return SinCosTable[360 - angle][1];
}

float fast_tan(int32_t angle)
{
	// return SinCosTable[angle][0]/SinCosTable[angle][1];
	return tanTable[angle];
}
